package solutions.leetcode.easy;

import solutions.lib.BaseSolution;

import java.util.LinkedList;
import java.util.List;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/">1380. 矩阵中的幸运数</a>
 */
public class Solution1380 extends BaseSolution {

    public List<Integer> luckyNumbers (int[][] matrix) {
        List<Integer> rst = new LinkedList<>();
        boolean[] flag = new boolean[matrix[0].length];
        for (int[] value : matrix) {
            int minV = value[0];
            int minP = 0;
            for (int j = 1; j < value.length; j++) {
                if (value[j] < minV) {
                    minV = value[j];
                    minP = j;
                }
            }
            if (flag[minP]) {
                continue;
            }
            boolean t = true;
            for (int[] ints : matrix) {
                if (ints[minP] > minV) {
                    t = false;
                    break;
                }
            }
            if (t) {
                rst.add(minV);
                flag[minP] = true;
            }
        }
        return rst;
    }

    public static void main(String[] args) {
        Solution1380 solution = new Solution1380();
        printVals(solution.luckyNumbers(new int[][]{{1,10,4,2},{9,3,8,7},{15,16,17,12}}));
    }
}